package com.nezha.lib_log

import com.nezha.lib_log.logger.EmailLogger
import com.nezha.lib_log.logger.FileLogger
import com.nezha.lib_log.logger.ILogger
import com.nezha.lib_log.logger.LogCatLogger
import com.nezha.lib_log.logger.NetLogger
import java.util.logging.Logger

class LoggerManager private  constructor(
            private val isDebug :Boolean,
            private val TAG:String,
            private val level:LoggerLevel,
            private val LoggerStrategy:LoggerStrategy,
            private val saveurl:String
){
    //**********策略模式************
    //3根据不同的策略创建不同的logger
    private var logger:ILogger=when(LoggerStrategy){
        com.nezha.lib_log.LoggerStrategy.LOGCAT-> LogCatLogger()
        com.nezha.lib_log.LoggerStrategy.FILE -> FileLogger()
        com.nezha.lib_log.LoggerStrategy.EMAIL -> EmailLogger()
        com.nezha.lib_log.LoggerStrategy.NET -> NetLogger()
    }
    init {
        logger.setLoggerLevel(level)
        logger.setDebug(isDebug)
        logger.setLogTAG(TAG)
        logger.setSaveUrl(saveurl)

    }
    //4提供打印日志方法
    fun v(tag:String,log:String){
        logger.v(tag,log)
    }
    fun d(tag:String,log:String){
        logger.d(tag,log)
    }
    fun i(tag:String,log:String){
        logger.i(tag,log)
    }
    fun w(tag:String,log:String){
        logger.w(tag,log)
    }
    fun e(tag:String,log:String){
        logger.e(tag,log)
    }

    //2次创建，build作为参数
    constructor(builder:Budiler): this(
                builder.isDebug,
                builder.TAG,
                builder.level,
                builder.loggerStrategy,
                builder.saveurl,
    )
    //**********构建者模式************
    //1定义构建类(定义静态内部类)
    class Budiler{
         var isDebug =false
         var TAG="默认TAG"
         var level=LoggerLevel.Verbose
         var loggerStrategy: LoggerStrategy = LoggerStrategy.LOGCAT
         var saveurl=""
        fun setTAG(TAG:String):Budiler{
            this.TAG=TAG
            return this
        }
        fun setDebug(isDebug:Boolean):Budiler{
            this.isDebug=isDebug
            return this
        }
        fun setLoggerLevel(level:LoggerLevel):Budiler{
            this.level=level
            return this
        }
        fun setLoggerStrategy(loggerStrategy:LoggerStrategy):Budiler{
            this.loggerStrategy=loggerStrategy
            return this
        }
        fun setSaveUrl(saveurl:String):Budiler{
            this.saveurl=saveurl
            return this
        }
        fun build(): LoggerManager {
            return LoggerManager(this)
        }
    }

}